﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;


namespace MSBTC.WinForms
{
    public partial class ClientListForm : Form
    {
        private string Datamanip;
        private int rowselect;
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Customer dbcus = new Customer();

        public ClientListForm()
        {
            InitializeComponent();
            
        }

        private void ClientListForm_Load(object sender, EventArgs e)
        {
            RefreshDB();
        }

      

        private void CList_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dlg = MessageBox.Show("You want to close the Client List Form?", "Warning", MessageBoxButtons.YesNo);
            if (dlg == DialogResult.No)
                e.Cancel = true;
            else if (dlg == DialogResult.No)
                e.Cancel = false;
        }

        #region functions
        private void RefreshDB()
        {
            var cus = (from c in dbcontext.Customers
                       select c).ToList();

            LVClientListInfo.Items.Clear();
            if (cus != null)
            {

                foreach (var sqlCus in cus)
                {
                    ListViewItem lvitems = LVClientListInfo.Items.Add(sqlCus.Customerid_PK.ToString());
                    lvitems.SubItems.Add(sqlCus.Customer_Name.ToString());
                    lvitems.SubItems.Add(sqlCus.Address.ToString());
                    lvitems.SubItems.Add(sqlCus.City.ToString());
                    lvitems.SubItems.Add(sqlCus.Description.ToString());
                    lvitems.SubItems.Add(sqlCus.Email_Address.ToString());
                    lvitems.SubItems.Add(sqlCus.Contacts.ToString());

                }
               
            }
            this.Refresh();
        }

  

        private void DisableFields()
        {
            //txtCname.Enabled = false;
            //txtAddr.Enabled = false;
            //txtCity.Enabled = false;
            //txtDesc.Enabled = false;
            //txtEadd.Enabled = false;
            //txtCnum.Enabled = false;

        }

        private void EnableFields()
        {
            //txtCname.Enabled = true;
            //txtAddr.Enabled = true;
            //txtCity.Enabled = true;
            //txtDesc.Enabled = true;
            //txtEadd.Enabled = true;
            //txtCnum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
             RefreshDB();

            //btnCNew.Enabled = true;
            //btnCDel.Enabled = false;
            //btnCEdit.Enabled = false;
            //btnClear.Enabled = true;
            //btnCSave.Enabled = false;
        }
        #endregion


        

        private void LVClientListInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            rowselect = LVClientListInfo.FocusedItem.Index;
            
            txtID.Text = LVClientListInfo.Items[rowselect].SubItems[0].Text;
            //txtCname.Text = LVClientListInfo.Items[rowselect].SubItems[1].Text;
            //txtAddr.Text = LVClientListInfo.Items[rowselect].SubItems[2].Text;
            //txtCity.Text = LVClientListInfo.Items[rowselect].SubItems[3].Text;
            //txtDesc.Text = LVClientListInfo.Items[rowselect].SubItems[4].Text;
            //txtEadd.Text = LVClientListInfo.Items[rowselect].SubItems[5].Text;
            //txtCnum.Text = LVClientListInfo.Items[rowselect].SubItems[6].Text;

            //btnCDel.Enabled = true;
            //btnCEdit.Enabled = true;
            //btnCNew.Enabled = true;
            //btnCSave.Enabled = false;
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
           
            var cus = (from c in dbcontext.Customers
                       where c.Customer_Name.Contains(txtSearch.Text)
                       select c).ToList();

            LVClientListInfo.Items.Clear();
            if (cus != null)
            {

                foreach (var sqlCus in cus)
                {
                    ListViewItem lvitems = LVClientListInfo.Items.Add(sqlCus.Customerid_PK.ToString());
                    lvitems.SubItems.Add(sqlCus.Customer_Name.ToString());
                    lvitems.SubItems.Add(sqlCus.Address.ToString());
                    lvitems.SubItems.Add(sqlCus.City.ToString());
                    lvitems.SubItems.Add(sqlCus.Description.ToString());
                    lvitems.SubItems.Add(sqlCus.Email_Address.ToString());
                    lvitems.SubItems.Add(sqlCus.Contacts.ToString());

                }
                
                if (txtSearch.Text == "")
                {
                    RefreshDB();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            ClientForm CForm = new ClientForm();
            CForm.Show();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("Do you want to Delete it?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.Customers.Find(Convert.ToInt32(txtID.Text));
                if (del != null)
                {

                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();
                }
                RefreshFields();
                RefreshDB();
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
